package com.yy.ant.sys.service.impl;

import com.yy.ant.sys.entity.Resources;
import com.yy.ant.sys.entity.RoleResources;
import com.yy.ant.sys.entity.User;
import com.yy.ant.sys.entity.UserRole;
import com.yy.ant.sys.mapper.ResourcesMapper;
import com.yy.ant.sys.mapper.RoleResourcesMapper;
import com.yy.ant.sys.mapper.UserRoleMapper;
import com.yy.ant.sys.service.IResourcesService;
import com.yy.model.Menu;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.yy.utils.SessionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liyingying
 * @since 2019-12-20
 */
@Service
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, Resources> implements IResourcesService {

	@Autowired
	private ResourcesMapper resourcesMapper;

	@Autowired
	private UserRoleMapper userRoleMapper;

	@Autowired
	private RoleResourcesMapper roleResourcesMapper;


	@Override
	public List<Menu> tree(String types,User sysUser) {
		QueryWrapper<Resources> queryWrapper=new QueryWrapper<>();
		if (SessionUtil.getAdmin(sysUser.getUname())){
			queryWrapper.lambda().eq(Resources::getTypes,types).eq(Resources::getStatus,0).orderByAsc(Resources::getSeq);
		}else {
			//根据用户权限查询按钮
			List<Long> list=new ArrayList<>();
			List<UserRole> userRoles=userRoleMapper.selectList(new QueryWrapper<UserRole>().eq("user_id",sysUser.getId()));
			for (UserRole userRole:userRoles){
				list.add(userRole.getRoleId());
			}
			List<Long> list1=new ArrayList<>();
			if (list!=null&&list.size()>0){
				List<RoleResources> roleResources=roleResourcesMapper.selectList(new QueryWrapper<RoleResources>().in("role_id",list));
				for (RoleResources o:roleResources){
					list1.add(o.getResourcesId());
				}
			}
			if (list1!=null&&list1.size()>0){
				queryWrapper.lambda().in(Resources::getId,list1).eq(Resources::getTypes,types).eq(Resources::getStatus,0).orderByAsc(Resources::getSeq);
			}else {
				return null;
			}
		}
		List<Menu> menuList = new ArrayList<Menu>();
		List<Resources>  l=resourcesMapper.selectList(queryWrapper);
		for (Resources o : l) {
            if (o.getPid() == 0) {
                Menu m1 = new Menu();
                m1.setId(o.getId());
                m1.setTitle(o.getTitle());
                m1.setHref(o.getUrl());
                m1.setIcon(o.getIcon());
                m1.setSeq(o.getSeq());
                m1.setCheck(false);
                m1.setSpread(false);
                m1.setClose(false);
                menuList.add(m1);
            }
        }
		Collections.sort(menuList, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				int diff = o1.getSeq() - o2.getSeq();
				if (diff > 0) {
					return 1;
				}else if (diff < 0) {
					return -1;
				}
				return 0;
			}
		}); 
		 // 为一级菜单设置子菜单，getChild是递归调用的
        for (Menu menu : menuList) {
            menu.setChildren(getChild(menu.getId(), l));
        }
        return menuList;
	}

	/**
	 * 递归查找子菜单
	 *
	 * @param id       当前菜单id
	 * @param rootMenu 要查找的列表
	 * @return
	 */
	private List<Menu> getChild(Long id, List<Resources> rootMenu) {
		// 子菜单
		List<Menu> childList = new ArrayList<>();
		for (Resources o : rootMenu) {
			// 遍历所有节点，将父菜单id与传过来的id比较
			if (StringUtils.isNotBlank(String.valueOf(o.getId()))) {
				if (id == o.getPid()) {
					Menu m1 = new Menu();
					m1.setId(o.getId());
					m1.setTitle(o.getTitle());
					m1.setHref(o.getUrl());
					m1.setIcon(o.getIcon());
					m1.setSeq(o.getSeq());
					m1.setCheck(false);
					m1.setSpread(false);
					m1.setClose(false);
					childList.add(m1);
				}
			}
		}
		Collections.sort(childList, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				int diff = o1.getSeq() - o2.getSeq();
				if (diff > 0) {
					return 1;
				}else if (diff < 0) {
					return -1;
				}
				// TODO Auto-generated method stub
				return 0;
			}
		}); 
		// 把子菜单的子菜单再循环一遍
		for (Menu menu : childList) {
			// 递归
			menu.setChildren(getChild(menu.getId(), rootMenu));
		}
		// 递归退出条件
		if (childList.size() == 0) {
			return null;
		}
		return childList;
	}
}
